<template>
  <div class="u-p-20">
    <el-card shadow="never">
      <div slot="header">
        <b class="u-f-16">
          <span v-if="objForm.id">编辑</span>
          <span v-else>添加</span>物流信息
        </b>
        <div class="u-right u-5mt">
          <el-button
            type="success"
            size="small"
            icon="el-icon-document-checked"
            @click="submitForm('objForm')"
            v-if="hasPerm('orderLogisticsPost')"
          >提交保存</el-button>
          <el-button size="small" icon="el-icon-back" @click="$router.go(-1)">返回</el-button>
        </div>
      </div>
      <el-form :model="objForm" :rules="rules" ref="objForm" label-width="100px" class="u-form">
        <el-form-item label="物流名称">
          <el-input v-model="objForm.order_logistics_name" placeholder="物流名称" disabled></el-input>
          <el-button type="text" @click="dialogVisible = true">选择物流</el-button>
        </el-form-item>

        <el-form-item label="物流编码">
          <el-input v-model="objForm.order_logistics_code" placeholder="物流编码" disabled></el-input>
        </el-form-item>

        <el-form-item label="物流单号">
          <el-input v-model="objForm.order_logistics_number" placeholder="物流单号"></el-input>
        </el-form-item>
      </el-form>

      <!-- 弹出框 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <logisticsIndex :isprops="true" @get_select="get_selects"></logisticsIndex>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import commonEdit from "@/mixins/common-edit";
import logisticsIndex from "@/views/logistics/index.vue";
export default {
  mixins: [commonEdit],
  components: {
    logisticsIndex
  },
  data() {
    return {
      apiName: "order",
      backRoute: "", //成功跳转地址
      objForm: {
        id: "",
        order_logistics_name: "",
        order_logistics_code: "",
        order_logistics_number: ""
      },
      dialogVisible: false
    };
  },
  methods: {
    async getInfo() {
      const self = this;
      await self.beforeGetInfo();
      self.$api[self.apiName].logisticsInfo(self.objForm.id).then(response => {
        if (response.code == 1) {
          self.objForm = response.data;
          self.afterGetInfo();
        }
      });
    },
    submitForm(formName) {
      const self = this;
      self.$refs[formName].validate(valid => {
        if (valid) {
          self.beforeSubmit();
          self.$api[self.apiName].saveLogistics(self.objForm).then(response => {
            if (response.code == 1) {
              self.$message({
                message: "操作成功",
                type: "success"
              });
              self.$router
                .push({
                  name: self.backRoute,
                  params: { page: self.currentPage }
                })
                .catch(() => {});
            }
          });
        } else {
          return false;
        }
      });
    },
    get_selects(row) {
      //获取物流选中数据
      window.console.log(row);
      this.objForm.order_logistics_name = row.logistics_name;
      this.objForm.order_logistics_code = row.logistics_code;
      this.dialogVisible = false; //关闭弹框
    }
  },
  created() {
    if (this.$route.params.id) {
      this.objForm.id = this.$route.params.id;
    }
  }
};
</script>
